//
//  HomeViewController.m
//  MyFramework
//
//  Created by 余洋 on 2018/5/8.
//  Copyright © 2018年 余洋. All rights reserved.
//

#import "HomeViewController.h"
#import "HomePictureCollectionViewCell.h"
#import "CarouselViewCollectionViewCell.h"

#import "HomeImageModel.h"

@interface HomeViewController ()

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray<HomeImageModel*> *modelList;

@end

@implementation HomeViewController

//cell的注册标识
static NSString *cellIdentifierForCarousel = @"CarouselViewCollectionViewCell";//轮播
static NSString *cellIdentifierForHomeImage = @"HomePictureCollectionViewCell";//轮播

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configurationCollectionView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - 配置CollectionView
- (void)configurationCollectionView {
    [self.collectionView registerClass:[CarouselViewCollectionViewCell class] forCellWithReuseIdentifier:cellIdentifierForCarousel];
    [self.collectionView registerNib:[UINib nibWithNibName:@"HomePictureCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:cellIdentifierForHomeImage];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    return self.modelList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        CarouselViewCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifierForCarousel forIndexPath:indexPath];
        cell.picArray = [NSMutableArray arrayWithObjects:@"http://tian2.murubang.cn/images/1_05.jpg",@"http://tian2.murubang.cn/images/1_03.jpg",@"https://static.firefoxchina.cn/img/201810/5_5bbaf214856b20.jpg",@"http://p1.ifengimg.com/2018_41/23F6EBD761D04120017330A7119BEEB1575AC395_w598_h320.jpg", nil];
//        cell.picArray = [NSMutableArray arrayWithObjects:@"http://tian2.murubang.cn/images/1_05.jpg", nil];
        return cell;
    } else {
        HomePictureCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifierForHomeImage forIndexPath:indexPath];
        cell.model = self.modelList[indexPath.item];
        return cell;
    }
}

//- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(nonnull NSString *)kind atIndexPath:(nonnull NSIndexPath *)indexPath {
//    return nil;
//}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return CGSizeMake(SCREEN_WIDTH, 200);
    }
    return CGSizeMake(SCREEN_WIDTH, 230);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    if (section == 0) {
        return UIEdgeInsetsZero;
    } else {
        return UIEdgeInsetsMake(8, 0, 0, 0);
    }
    
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
//    if (section == 0) {
//        return CGSizeMake(0, 0);
//    }
//    return CGSizeMake(0, 0);
//}
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
//    if (section == 0) {
//        return CGSizeMake(0, 0);
//    } else {
//        return CGSizeMake(0, 0);
//    }
//}

#pragma mark 懒加载
- (NSMutableArray<HomeImageModel *> *)modelList {
    if (_modelList == nil) {
        //设置假数据
        HomeImageModel *model1 = [HomeImageModel new];
        model1.bgImageName = @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1531911968591&di=f1bf1371cc446f75152057e0772ed9d3&imgtype=0&src=http%3A%2F%2Fimg.news.d.cn%2Fupimg%2F1803%2Fhoatjet5x6w3.jpg";
        model1.title = @"黑暗料理王";
        model1.subtitle = @"叮咚！今日份的料理已送达";
        model1.iconName = @"";
        
        _modelList = [NSMutableArray arrayWithObjects:model1, nil];
    }
    return _modelList;
}

@end
